package org.hhchat.leetcode.hashtable;

import java.util.*;

/**
 * Created this one by HMH on 2017/9/29.
 */
public class code438_easy {

    static class Solution {
        public List<Integer> findAnagrams(String s, String p) {
            int[] hash = new int[256];
            for(int i=0;i<p.length();i++) {
                hash[p.charAt(i)]++;
            }
            int count = p.length();
            int l = 0, r = 0;
            List<Integer> list = new ArrayList<>();
            while (r < s.length()) {
                if (--hash[s.charAt(r++)] >= 0) {
                    count--;
                }
                if (count == 0) {
                    list.add(l);
                }
                if (r - l == p.length() && hash[s.charAt(l++)]++ >= 0) {
                    count++;
                }
            }
            return list;
        }


    }

    public static void main(String[] args){
        Solution solution = new Solution();
        System.out.println(solution.findAnagrams("abab", "ab"));
    }

}
